; Generated by Modix - Version 3.6.3 Macro A

var probing_tool = 0.000
; This allows you to compensate for whatever tool you use to measure the nozzle distance.
; It is set to 0.000mm by default, you can set this to the thickness of the tool or paper you use for the Z-offset calibration
; A sheet of paper is on average 0.05 to 0.10mm thick.


M291 S3 R"Setting the Z-offset Height" P"Press OK to continue, or CANCEL to abort"
if {global.idex} = 1
	M291 S2 R"Warning: Secondary Z-offset" P"The secondary Z-offset has been reset to stock values. You must re-calibrate the secondary Z-offset values after completing this calibration"

G31 Z0																	; set Z-offset to Z=0
echo >"0:/sys/config_probe.g" "G31 Z0"
if {global.idex} = 1
	G10 P1 Z0															; set secondary Z-offset to Z=0
	echo >"0:/sys/config_probe_secondary.g" "G10 P1 Z0"					; Set the Z-offset to 0 before starting

M291 T0 S4 R"Hotend Temp" P"Select the temperature you wish to run the Z-offset procedure at" K{"0 (off)","210","230","250","270"} 
	if input = 0
		M568 P0 S0 R0 A0
	if input = 1
		M568 P0 S210 R210 A2
	if input = 2
		M568 P0 S230 R230 A2
	if input = 3
		M568 P0 S250 R250 A2
	if input = 4
		M568 P0 S270 R270 A2

G28
T0 																		; Select T1
M116 P0 S5

G29 S2																	; cancel mesh bed compensation
M290 R0 S0																; cancel baby stepping
G90																		; absolute movements

G1 Z10																	; move the nozzle up
G1 X100 Y200 F3000														; move to X100, Y100
G4 P0																	; wait for the movement to stop

M300 S1111 P666															; beep
M564 H1 S0																; ignore axis limits
M291 S2 R"Test Z Probe Trigger Height - Step 1" P"Place a paper sheet under the nozzle and raise the bed until slight friction can be noticed" Z1 ;

G92 Z0																	; the nozzle should now be just touching the bed so set the logical Z position to match the physical Z position
M300 S666 P666															; beep
G1 Z8 F300																; insure Z position will allow for probing

G91																		; relative moves
G1 X{sensors.probes[0].offsets[0] * -1} Y{sensors.probes[0].offsets[1] * -1} F1000 ; move nozzle so that BLTouch is in it's position 
G90																		; absolute moves

G4 P0																	; wait for the movement to stop

M291 S2 R"Test Z Probe Trigger Height - Step 2" P"Please remove the paper sheet and wait for the bed-probing to begin" ;
G30 S-1																	; probe the bed and report the Z probe trigger height
M564 S1 H1																; respect axis limits

var Z_auto_offset = {sensors.probes[0].lastStopHeight+var.probing_tool} ; math to calculate the BLTouch offset
echo >"0:/sys/config_probe.g" "G31 Z"^{var.Z_auto_offset}				; write the Z-offset to the config_probe.g file

echo "the Z-offset has been stored in the config_probe.g file. The Z-offset is", var.Z_auto_offset, "mm"

M568 P0 S0 R0 A0														; set temperature back to 0, disable heaters
G4 P0																	; wait for the movement to stop
G91																		; relative movements
G1 Z10 F300																; Lift up the nozzle again so it's not pressed against the bed surface
G90																		; absolute movements
G1 Y0 F4000																; move nozzle to Y=0
T-1																		; Deselect T0
G4 P0																	; wait for the movement to stop
M18																		; release stepper motors